package com.afkt.project.slice;


import com.afkt.project.model.item.ButtonList;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.adapter.ButtonAdapter;
import com.afkt.project.util.SkipUtils;
import com.afkt.project.util.ToastUtil;
import com.dev.engine.log.DevLogEngine;
import com.dev.environment.AdapterItem;
import com.dev.environment.DevEnvironment;
import com.dev.environment.DevEnvironmentAbilitySlice;
import com.dev.environment.Utils;
import com.dev.environment.bean.EnvironmentBean;
import com.dev.environment.bean.ModuleBean;
import com.dev.environment.listener.OnEnvironmentChangeListener;
import com.dev.utils.app.toast.ToastTintUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import static com.afkt.project.common.DevFinal.TAG;

public class DevEnvironmentLibSlice extends AbilitySlice {


    private DirectionalLayout bar_layout;

    private Image ui_return;

    private DirectionalLayout ui_bar;

    private ListContainer UI_list;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);


        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_bar_layout);
        ui_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ui_bar, null, false);
        Text text = (Text) ui_bar.findComponentById(ResourceTable.Id_title_name);
        text.setText("DevEnvironment 环境配置切换库");

        bar_layout.addComponent(ui_bar);
        UI_list = (ListContainer) findComponentById(ResourceTable.Id_UI_List);
        ui_return = (Image) ui_bar.findComponentById(ResourceTable.Id_ui_return);
        ui_return.setClickedListener(component -> onBackPressed());
        ButtonAdapter buttonAdapter = new ButtonAdapter(ButtonList.getModuleDevEnvironmentButtonValues(), this);
        UI_list.setItemProvider(buttonAdapter);

        UI_list.setItemClickedListener((listContainer, component, i, l) -> {
            ButtonValue buttonValue = (ButtonValue) UI_list.getItemProvider().getItem(i);
            boolean result;
            switch (buttonValue.type) {
                case ButtonValue.BTN_DEV_ENVIRONMENT:
                    SkipUtils.startActivity(new DevEnvironmentAbilitySlice(() -> {
                        // 环境改变通知
                        DevEnvironment.addOnEnvironmentChangeListener((module, oldEnvironment, newEnvironment) -> {
                            // 可以进行重新请求等操作

                            StringBuilder builder = new StringBuilder();
                            builder.append("module");
                            builder.append("\nname: ").append(module.getName());
                            builder.append("\nalias: ").append(module.getAlias());
                            builder.append("\n\n");
                            builder.append("oldEnvironment");
                            builder.append("\nname: ").append(oldEnvironment.getName());
                            builder.append("\nvalue: ").append(oldEnvironment.getValue());
                            builder.append("\nalias: ").append(oldEnvironment.getAlias());
                            builder.append("\n\n");
                            builder.append("newEnvironment");
                            builder.append("\nname: ").append(newEnvironment.getName());
                            builder.append("\nvalue: ").append(newEnvironment.getValue());
                            builder.append("\nalias: ").append(newEnvironment.getAlias());
                            ToastTintUtils.normal(builder.toString());
                            DevLogEngine.getEngine().dTag(TAG, builder.toString());
                        });
                    }), buttonValue);
                    break;
                case ButtonValue.BTN_USE_CUSTOM: //
                    EnvironmentBean custom = new EnvironmentBean("自定义配置",
                            "https://custom.com", "动态自定义", DevEnvironment.getServiceModule());
                    result = DevEnvironment.setServiceEnvironment(DevEnvironmentLibSlice.this, custom);
                    ToastUtil.toastShowLong(DevEnvironmentLibSlice.this, result ? "设置成功" : "设置失败", ToastUtil.TOAST_SHORT_TIME);
                    break;

            }
        });

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(ui_bar);
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
